#pragma once
#include<iostream>
#include <pthread.h>
static int i;//只能在自己所在的源文件内部使用，自动被初始化为0
typedef void(*call_back_t)(int);
class thread
{

    private:
    pthread_t tid_;
    std::string thread_name;
    uint64_t start_timestamp;//开始时间戳
    bool running;//是否正在运行
    call_back_t cb_;
    int x_;
    public:
    static void* routine(void*args)
    {
        thread*t=static_cast<thread*>(args);
        t->entry();
    }
    public:

    thread(call_back_t fun,int x)
    :tid_(0),thread_name(""),start_timestamp(0),running(0),cb_(fun),x_(x)
    {
       
    }
    void run()
    {
         thread_name="thread-"+std::to_string(i++);
        running=true;
        start_timestamp=time(nullptr);
        pthread_create(&tid_,nullptr,routine,this);
    }
    void join()
    {
        pthread_join(tid_,nullptr);
    }
    void entry()//入口函数
    {
        cb_(x_);
    }
    std::string threadname()
    {
        return thread_name;
    }
    uint64_t threadrunningtime()
    {
        return start_timestamp;
    }
    bool is_running()
    {
        return running;
    }
    ~thread()
    {}
};